package com.proposition.support.aop;

import com.alibaba.fastjson.JSON;
import com.proposition.commons.model.SysUserLog;
import com.proposition.commons.model.User;
import com.proposition.logger.service.SysUserLogService;
import com.proposition.support.annotation.LogAnnotation;
import com.proposition.support.utils.IpUtils;
import com.proposition.support.utils.RemoteClientIpUtils;
import com.proposition.support.utils.UUIDUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.WebSubject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2017-11-10 10:27
 **/
@Aspect
@Component
public class WebLogAspect {
	protected static org.slf4j.Logger logger = LoggerFactory.getLogger(WebLogAspect.class);

	@Pointcut("@annotation(com.proposition.support.annotation.LogAnnotation)")
	public void webLog() {
	}

	@Autowired
	private SysUserLogService sysUserLogService;


	/**
	 * 方法正常完成后执行方法
	 *
	 * @param point
	 */
	@Before("webLog()")
	public void afterReturning(JoinPoint point) {
		try {
			Subject subject = null;
			try {
				subject = SecurityUtils.getSubject();
			} catch (Exception e) {
				e.printStackTrace();
				//发生异常则不进行
				return;
			}
			SysUserLog userLog = new SysUserLog();
			String targetName = point.getTarget().getClass().getName();
			Class targetClass = Class.forName(targetName);
			String methodName = point.getSignature().getName();
			Method[] method = targetClass.getMethods();
			Object[] params = point.getArgs(); // 获得参数列表
			for (Method m : method) {
				if (m.getName().equals(methodName)) {
					Class[] tmpCs = m.getParameterTypes();
					if (tmpCs.length == params.length) {
						// 获取注解内容
						LogAnnotation logAnnotation = m.getAnnotation(LogAnnotation.class);
						if (logAnnotation != null) {
							//写入参数
							if (params.length > 0) {
								// 使用json转换工具 将参数转为json串，以便存入数据库
								String jsonStr = JSON.toJSONString(params);
								userLog.setParams(jsonStr);
							}
							//获取模块名称
							String moduleName = logAnnotation.moduleName();
							//获取操作名称
							String operate = logAnnotation.operate();
							userLog.setModuleName(moduleName);
							userLog.setOperate(operate);
							userLog.setClassName(targetName);
							userLog.setMethodName(methodName);
							userLog.setLogId(UUIDUtil.getUUID());
							userLog.setTime(new Date());
							HttpServletRequest request = (HttpServletRequest)((WebSubject)subject).getServletRequest();
							userLog.setIp(IpUtils.getIpFromRequest(request));
							User user =  (User) SecurityUtils.getSubject().getPrincipal();
							userLog.setUsername(user.getUserName());
							sysUserLogService.addUserLogInfo(userLog);
							break;
						}
					}
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
